# Makefile for oc_contributor_zhanghaibo - Go 应用容器化构建工具

# 变量定义
TOOL_NAME = go_pack
PROJECT_NAME=oc_contributor_zhanghaibo
INSTALL_DIR = /usr/local/bin
PYTHON_MODULE_DIR = /usr/local/lib/python3/dist-packages/$(PROJECT_NAME)
CURRENT_DIR = $(shell pwd)
PYTHON = python3

# 默认目标
.PHONY: all
all: help

# 显示帮助信息
.PHONY: help
help:
	@echo "Go 应用容器化构建工具安装脚本"
	@echo ""
	@echo "可用命令:"
	@echo "  make install     - 安装工具到系统"
	@echo "  make uninstall   - 卸载工具"
	@echo "  make install-dev - 开发模式安装（符号链接）"
	@echo "  make test        - 运行基本测试"
	@echo "  make clean       - 清理临时文件"
	@echo "  make check-deps  - 检查依赖"
	@echo ""
	@echo "安装后可以直接使用: $(TOOL_NAME) <project_path> [options]"

# 检查依赖
.PHONY: check-deps
check-deps:
	@echo "检查依赖..."
	@$(PYTHON) --version || (echo "错误: 需要 Python 3" && exit 1)
	@command -v docker >/dev/null || (echo "错误: 需要 Docker" && exit 1)
	@$(PYTHON) -c "import jinja2" 2>/dev/null || (echo "错误: 需要安装 jinja2: pip3 install jinja2" && exit 1)
	@echo "依赖检查通过"

# 安装到系统
.PHONY: install
install: check-deps
	@echo "安装 $(TOOL_NAME) 到系统..."
	sudo mkdir -p $(PYTHON_MODULE_DIR)
	sudo cp -r src/* $(PYTHON_MODULE_DIR)/
	sudo touch $(PYTHON_MODULE_DIR)/__init__.py
	@echo '#!/usr/bin/env python3' | sudo tee $(INSTALL_DIR)/$(TOOL_NAME) > /dev/null
	@echo 'import sys' | sudo tee -a $(INSTALL_DIR)/$(TOOL_NAME) > /dev/null
	@echo 'import os' | sudo tee -a $(INSTALL_DIR)/$(TOOL_NAME) > /dev/null
	@echo 'sys.path.insert(0, "/usr/local/lib/python3/dist-packages")' | sudo tee -a $(INSTALL_DIR)/$(TOOL_NAME) > /dev/null
	@echo 'from oc_contributor_zhanghaibo.__main__ import main' | sudo tee -a $(INSTALL_DIR)/$(TOOL_NAME) > /dev/null
	@echo 'if __name__ == "__main__":' | sudo tee -a $(INSTALL_DIR)/$(TOOL_NAME) > /dev/null
	@echo '    main()' | sudo tee -a $(INSTALL_DIR)/$(TOOL_NAME) > /dev/null
	sudo chmod +x $(INSTALL_DIR)/$(TOOL_NAME)
	@echo "安装完成！现在可以使用: $(TOOL_NAME) <project_path>"
	@echo "查看帮助: $(TOOL_NAME) --help"

# 开发模式安装（使用符号链接）
.PHONY: install-dev
install-dev: check-deps
	@echo "开发模式安装 $(TOOL_NAME)..."
	sudo mkdir -p $(dir $(PYTHON_MODULE_DIR))
	sudo ln -sfn $(CURRENT_DIR)/src $(PYTHON_MODULE_DIR)
	@echo '#!/usr/bin/env python3' | sudo tee $(INSTALL_DIR)/$(TOOL_NAME) > /dev/null
	@echo 'import sys' | sudo tee -a $(INSTALL_DIR)/$(TOOL_NAME) > /dev/null
	@echo 'import os' | sudo tee -a $(INSTALL_DIR)/$(TOOL_NAME) > /dev/null
	@echo 'sys.path.insert(0, "/usr/local/lib/python3/dist-packages")' | sudo tee -a $(INSTALL_DIR)/$(TOOL_NAME) > /dev/null
	@echo 'from oc_contributor_zhanghaibo.__main__ import main' | sudo tee -a $(INSTALL_DIR)/$(TOOL_NAME) > /dev/null
	@echo 'if __name__ == "__main__":' | sudo tee -a $(INSTALL_DIR)/$(TOOL_NAME) > /dev/null
	@echo '    main()' | sudo tee -a $(INSTALL_DIR)/$(TOOL_NAME) > /dev/null
	sudo chmod +x $(INSTALL_DIR)/$(TOOL_NAME)
	@echo "开发模式安装完成！代码更改会立即生效"
	@echo "使用: $(TOOL_NAME) <project_path>"

# 卸载
.PHONY: uninstall
uninstall:
	@echo "卸载 $(TOOL_NAME)..."
	sudo rm -f $(INSTALL_DIR)/$(TOOL_NAME)
	sudo rm -rf $(PYTHON_MODULE_DIR)
	@echo "卸载完成"

# 基本测试
.PHONY: test
test:
	@echo "运行基本测试..."
	@if command -v $(TOOL_NAME) >/dev/null 2>&1; then \
		echo "测试命令可用性..."; \
		$(TOOL_NAME) --help >/dev/null && echo "✓ 帮助命令正常"; \
	else \
		echo "✗ 工具未安装或不在 PATH 中"; \
		exit 1; \
	fi
	@echo "基本测试通过"

# 清理临时文件
.PHONY: clean
clean:
	@echo "清理临时文件..."
	find . -name "*.pyc" -delete
	find . -name "__pycache__" -type d -exec rm -rf {} + 2>/dev/null || true
	find . -name "*.egg-info" -type d -exec rm -rf {} + 2>/dev/null || true
	@echo "清理完成"

# 用户安装（不需要 sudo）
.PHONY: install-user
install-user: check-deps
	@echo "用户模式安装 $(TOOL_NAME)..."
	mkdir -p $(HOME)/.local/bin
	mkdir -p $(HOME)/.local/lib/python3/site-packages/$(PROJECT_NAME)  
	cp -r src/* $(HOME)/.local/lib/python3/site-packages/$(PROJECT_NAME)/  
	touch $(HOME)/.local/lib/python3/site-packages/$(PROJECT_NAME)/__init__.py  
	@echo '#!/usr/bin/env python3' > $(HOME)/.local/bin/$(TOOL_NAME)
	@echo 'import sys' >> $(HOME)/.local/bin/$(TOOL_NAME)
	@echo 'import os' >> $(HOME)/.local/bin/$(TOOL_NAME)
	@echo 'sys.path.insert(0, "$(HOME)/.local/lib/python3/site-packages")' >> $(HOME)/.local/bin/$(TOOL_NAME)
	@echo 'from $(PROJECT_NAME).__main__ import main' >> $(HOME)/.local/bin/$(TOOL_NAME)  # 使用变量
	@echo 'if __name__ == "__main__":' >> $(HOME)/.local/bin/$(TOOL_NAME)
	@echo '    main()' >> $(HOME)/.local/bin/$(TOOL_NAME)
	chmod +x $(HOME)/.local/bin/$(TOOL_NAME)
	@echo "用户模式安装完成！"
	@echo "请确保 $(HOME)/.local/bin 在你的 PATH 中"
	@echo "可以运行: echo 'export PATH=\"$(HOME)/.local/bin:\$$PATH\"' >> ~/.bashrc"
	@echo "然后重新加载: source ~/.bashrc"

# 卸载用户安装
.PHONY: uninstall-user
uninstall-user:
	@echo "卸载用户模式的 $(TOOL_NAME)..."
	rm -f $(HOME)/.local/bin/$(TOOL_NAME)
	rm -rf $(HOME)/.local/lib/python3/site-packages/$(PROJECT_NAME)
	@echo "用户模式卸载完成"

# 显示安装信息
.PHONY: info
info:
	@echo "工具信息:"
	@echo "  名称: $(TOOL_NAME)"
	@echo "  安装目录: $(INSTALL_DIR)"
	@echo "  模块目录: $(PYTHON_MODULE_DIR)"
	@echo "  当前目录: $(CURRENT_DIR)"
	@echo ""
	@if command -v $(TOOL_NAME) >/dev/null 2>&1; then \
		echo "状态: 已安装"; \
	else \
		echo "状态: 未安装"; \
	fi

# 创建分发包
.PHONY: package
package: clean
	@echo "创建分发包..."
	tar -czf $(TOOL_NAME).tar.gz \
		--exclude='.git*' \
		--exclude='*.tar.gz' \
		--exclude='__pycache__' \
		--exclude='*.pyc' \
		src/ Makefile README.md docs/ 2>/dev/null || \
	tar -czf $(TOOL_NAME).tar.gz \
		--exclude='.git*' \
		--exclude='*.tar.gz' \
		--exclude='__pycache__' \
		--exclude='*.pyc' \
		src/ Makefile
	@echo "分发包已创建: $(TOOL_NAME).tar.gz"